********************************************************************************
clear all
set more off
capture log close

********************************************************************************
** File paths
** Set the current directory to the same folder where the code is

global input "../input" 
global output "../output" 
global temp "../temp"

********************************************************************************
** Start log, save it into temp folder
log using "$temp/figures_length_edits", replace

********************************************************************************
use $temp/pagelength_users_edits, clear

sum vMonth if vMonth == tm(2004m1) 
local l2004m1 = `r(mean)'
sum vMonth if vMonth == tm(2016m1) 
local l2016m1 = `r(mean)'
sum vMonth if vMonth == tm(2014m8) 
local l2014m8 = `r(mean)'

********************************************************************************
** Length: main graph
********************************************************************************
preserve
keep if vLanguage!="nl"

collapse (mean) vLength, by(vTreatmentGroup vMonth)
reshape wide vLength, i(vMonth) j(vTreatmentGroup)
gen vDif = vLength1 - vLength0
sum vLength1 vLength0 vDif

twoway line vLength1 vLength0 vDif vMonth, xline(`l2014m8', lcolor(gs8) lpattern(dash)) graphregion(fcolor(gs16)) scheme(s2mono) lcolor(green gs0 gs0) lpattern(solid solid dash) xlabel(`l2004m1'(36)`l2016m1', valuelabel labsize(medlarge)) ylabel(0(4000)12000, labsize(medlarge)) ytitle("Average page length" , size(medlarge)) xtitle(,size(medlarge)) legend(label(1 "Treatment") label(2 "Control") label(3 "Treatment - Control") size(medlarge)) 
graph export $output/gLength.pdf, replace 

restore

********************************************************************************
** Length: by language
********************************************************************************
foreach lLang in fr de it nl { 
	display "Language `lLang'"
	
	preserve
	keep if vLanguage=="`lLang'"

	if vLanguage=="fr" {
		local lColor blue
		local lYStep = 5000
		local lYMax = 15000
	}
	else if vLanguage=="de" {
		local lColor red
		local lYStep = 5000
		local lYMax = 15000
	}
	else if vLanguage=="it" {
		local lColor purple
		local lYStep = 2000
		local lYMax = 6000
	}
	else if vLanguage=="nl" {
		local lColor orange
		local lYStep = 2000
		local lYMax = 6000
	}

	collapse (mean) vLength, by(vTreatmentGroup vMonth)
	reshape wide vLength, i(vMonth) j(vTreatmentGroup)
	gen vDif = vLength1 - vLength0
	sum vLength1 vLength0 vDif

	twoway line vLength1 vLength0 vDif vMonth, xline(`l2014m8', lcolor(gs8) lpattern(dash)) graphregion(fcolor(gs16)) scheme(s2mono) lcolor(`lColor' gs0 gs0) lpattern(solid solid dash) xlabel(`l2004m1'(36)`l2016m1', valuelabel labsize(medlarge)) ylabel(0(`lYStep')`lYMax', labsize(medlarge)) ytitle("Average page length" , size(medlarge)) xtitle(,size(medlarge)) legend(label(1 "Treatment") label(2 "Control") label(3 "Treatment - Control") size(medlarge)) 
	graph export $output/gLength_`lLang'.pdf, replace 

	restore

}

********************************************************************************
** Other length measures: images plain
********************************************************************************
preserve
keep if vLanguage!="nl"

collapse (mean) images plain, by(vTreatmentGroup vMonth)
reshape wide images plain, i(vMonth) j(vTreatmentGroup)
foreach y in images plain {
	gen vDif_`y' = `y'1 - `y'0
}

twoway line images1 images0 vDif_images vMonth, xline(`l2014m8', lcolor(gs8) lpattern(dash)) graphregion(fcolor(gs16)) scheme(s2mono) lcolor(green gs0 gs0) lpattern(solid solid dash) xlabel(`l2004m1'(36)`l2016m1', valuelabel labsize(medlarge)) ylabel(0(5)20, labsize(medlarge)) ytitle("Average number of images" , size(medlarge)) xtitle(,size(medlarge)) legend(label(1 "Treatment") label(2 "Control") label(3 "Treatment - Control") size(medlarge)) 
graph export $output/gImages.pdf, replace 

twoway line plain1 plain0 vDif_plain vMonth, xline(`l2014m8', lcolor(gs8) lpattern(dash)) graphregion(fcolor(gs16)) scheme(s2mono) lcolor(green gs0 gs0) lpattern(solid solid dash) xlabel(`l2004m1'(36)`l2016m1', valuelabel labsize(medlarge)) ylabel(0(4000)12000, labsize(medlarge)) ytitle("Average length of plain text" , size(medlarge)) xtitle(,size(medlarge)) legend(label(1 "Treatment") label(2 "Control") label(3 "Treatment - Control") size(medlarge)) 
graph export $output/gLengthPlainText.pdf, replace 

restore

********************************************************************************
** Length: without Cordoba in French, where a single user increased length a lot
********************************************************************************
preserve
keep if vLanguage!="nl"
drop if vPage =="Q5818" & vLanguage=="fr"

collapse (mean) vLength, by(vTreatmentGroup vMonth)
reshape wide vLength, i(vMonth) j(vTreatmentGroup)
gen vDif = vLength1 - vLength0
sum vLength1 vLength0 vDif

twoway line vLength1 vLength0 vDif vMonth, xline(`l2014m8', lcolor(gs8) lpattern(dash)) graphregion(fcolor(gs16)) scheme(s2mono) lcolor(green gs0 gs0) lpattern(solid solid dash) xlabel(`l2004m1'(36)`l2016m1', valuelabel labsize(medlarge)) ylabel(0(4000)12000, labsize(medlarge)) ytitle("Average page length" , size(medlarge)) xtitle(,size(medlarge)) legend(label(1 "Treatment") label(2 "Control") label(3 "Treatment - Control") size(medlarge)) 
graph export $output/gLength_wo_FrenchCordoba.pdf, replace 

restore

********************************************************************************
** Log length
********************************************************************************

preserve
keep if vLanguage!="nl"

** Drop log length which is calculated without added 1, does not make a difference in the regressions when all pages have strictly positive length, but here when pages start on the figure want to add 1, to get log 0, and take the mean over all pages
drop vLogLength
gen vLogLength = log(vLength + 1)
collapse (mean) vLogLength, by(vTreatmentGroup vMonth)
reshape wide vLogLength, i(vMonth) j(vTreatmentGroup)
gen vDif = vLogLength1 - vLogLength0

twoway line vLogLength1 vLogLength0 vDif vMonth, xline(`l2014m8', lcolor(gs8) lpattern(dash)) graphregion(fcolor(gs16)) scheme(s2mono) lcolor(green gs0 gs0) lpattern(solid solid dash) xlabel(`l2004m1'(36)`l2016m1', valuelabel labsize(medlarge)) ylabel(0(2)10, labsize(medlarge)) ytitle("Average of log. page length" , size(medlarge)) xtitle(,size(medlarge)) legend(label(1 "Treatment") label(2 "Control") label(3 "Treatment - Control") size(medlarge)) 
graph export $output/gLogLength.pdf, replace 

restore

********************************************************************************
** Number of users, edit distance
********************************************************************************
preserve
keep if vLanguage!="nl"

collapse (mean) vEditDays vUsers vEditDist, by(vTreatmentGroup vMonth)
reshape wide vEditDays vUsers vEditDist, i(vMonth) j(vTreatmentGroup)

twoway line vUsers1 vUsers0 vMonth, xline(`l2014m8', lcolor(gs8) lpattern(dash)) graphregion(fcolor(gs16)) scheme(s2mono) lcolor(green gs0) lpattern(solid dash) xlabel(`l2004m1'(36)`l2016m1', valuelabel labsize(medlarge)) ylabel(0(0.2)1, labsize(medlarge)) ytitle("Average number of users" , size(medlarge)) xtitle(,size(medlarge)) legend(label(1 "Treatment") label(2 "Control") size(medlarge)) 
graph export $output/gUsers.pdf, replace 

twoway line vEditDays1 vEditDays0 vMonth, xline(`l2014m8', lcolor(gs8) lpattern(dash)) graphregion(fcolor(gs16)) scheme(s2mono) lcolor(green gs0) lpattern(solid dash) xlabel(`l2004m1'(36)`l2016m1', valuelabel labsize(medlarge)) ylabel(0(0.2)1, labsize(medlarge)) ytitle("Average number of edits" , size(medlarge)) xtitle(,size(medlarge)) legend(label(1 "Treatment") label(2 "Control") size(medlarge)) 
graph export $output/gEditDays.pdf, replace 

twoway line vEditDist1 vEditDist0 vMonth, xline(`l2014m8', lcolor(gs8) lpattern(dash)) graphregion(fcolor(gs16)) scheme(s2mono) lcolor(green gs0) lpattern(solid dash) xlabel(`l2004m1'(36)`l2016m1', valuelabel labsize(medlarge)) ylabel(, labsize(medlarge)) ytitle("Average edit distance" , size(medlarge)) xtitle(,size(medlarge)) legend(label(1 "Treatment") label(2 "Control") size(medlarge)) 
graph export $output/gEditDist.pdf, replace 

restore

********************************************************************************
log close
clear
